﻿using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x02000405 RID: 1029
public class AttachBoneWeight
{
	// Token: 0x0600199F RID: 6559 RVA: 0x000A51A8 File Offset: 0x000A33A8
	public static void Attach(GameObject baseBoneRoot, GameObject attachMeshRoot, bool includeInactive)
	{
		Dictionary<string, Transform> bones = new Dictionary<string, Transform>();
		AttachBoneWeight.ListupBones(ref bones, baseBoneRoot.transform, includeInactive);
		AttachBoneWeight.SetupRenderers(bones, attachMeshRoot, includeInactive);
	}

	// Token: 0x060019A0 RID: 6560 RVA: 0x000A51D4 File Offset: 0x000A33D4
	private static void ListupBones(ref Dictionary<string, Transform> bones, Transform trans, bool includeInactive)
	{
		if (trans == null || trans.gameObject == null)
		{
			MonoBehaviour.print("無効な骨");
			return;
		}
		bool flag = trans.gameObject.activeSelf && trans.gameObject.activeInHierarchy;
		bool flag2 = includeInactive || flag;
		if (flag2 && !bones.ContainsKey(trans.name))
		{
			bones.Add(trans.name, trans);
		}
		for (int i = 0; i < trans.childCount; i++)
		{
			AttachBoneWeight.ListupBones(ref bones, trans.GetChild(i), includeInactive);
		}
	}

	// Token: 0x060019A1 RID: 6561 RVA: 0x000A5284 File Offset: 0x000A3484
	private static void SetupRenderers(Dictionary<string, Transform> bones, GameObject attachObj, bool includeInactive)
	{
		SkinnedMeshRenderer[] componentsInChildren = attachObj.GetComponentsInChildren<SkinnedMeshRenderer>(includeInactive);
		foreach (SkinnedMeshRenderer renderer in componentsInChildren)
		{
			AttachBoneWeight.SetupRenderer(bones, renderer);
		}
	}

	// Token: 0x060019A2 RID: 6562 RVA: 0x000A52BC File Offset: 0x000A34BC
	private static void SetupRenderer(Dictionary<string, Transform> bones, SkinnedMeshRenderer renderer)
	{
		string name = renderer.rootBone.name;
		if (!bones.ContainsKey(name))
		{
			GlobalData.Debug_LogWarning("我们找不到根本:" + name);
			return;
		}
		Transform rootBone = bones[name];
		renderer.rootBone = rootBone;
		Transform[] array = new Transform[renderer.bones.Length];
		for (int i = 0; i < renderer.bones.Length; i++)
		{
			Transform transform = renderer.bones[i];
			string name2 = transform.name;
			if (bones.ContainsKey(name2))
			{
				array[i] = bones[name2];
			}
			else
			{
				Transform parent = renderer.bones[i].parent;
				if (parent != null && bones.ContainsKey(parent.name))
				{
					transform.SetParent(bones[parent.name]);
				}
				GlobalData.Debug_LogWarning("我找不到可以附接的骨管:" + name2);
				array[i] = null;
			}
		}
		renderer.bones = array;
	}
}
